﻿Public Class ExcelDAO
    Public Sub importToServer(ByVal ExcelPath As String, ByVal InsertedTableName As String)

        Try
            Dim ExceCon As String = _
                "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ExcelPath & "; Extended Properties=Excel 8.0"
            Dim excelConnection As System.Data.OleDb.OleDbConnection = _
                New System.Data.OleDb.OleDbConnection(ExceCon)
            excelConnection.Open()
            Dim OleStr As String = "SELECT * INTO [ODBC; Driver={SQL Server};" & _
                 "Server=./SQLEXPRESS;Database=QUANLYTHUENHA;Uid=;Pwd=; ].[" & InsertedTableName & "]     FROM [Sheet1$];"

            'Dim OleStr As String = "SELECT * INTO [ODBC; Driver={SQL Server};Server=" _
            '    & ServerName & ";Database=" & DBName & ";Uid=" & _
            '    UserName & ";Pwd=" & Password & "; ].[" & _
            '    InsertedTableName & "]   FROM [Sheet1$];"

            Dim excelCommand As New System.Data.OleDb.OleDbCommand(OleStr, excelConnection)
            excelCommand.ExecuteNonQuery()
            excelConnection.Close()
        Catch ex As Exception
            Throw New Exception("Error: " & ex.Message)
        End Try
    End Sub

End Class
